package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.utils.common.SensitiveWordUtil;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    /**
     * 新增敏感词
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        //参数校验
        if (wmSensitive == null || StringUtils.isBlank(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (extracted(wmSensitive)) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    private boolean extracted(WmSensitive wmSensitive) {
        //数据处理
        //敏感词已存在不能保存
        LambdaQueryWrapper<WmSensitive> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WmSensitive::getSensitives, wmSensitive.getSensitives());
        WmSensitive sensitive = getOne(lqw);
        if (sensitive != null) {
            return true;
        }
        wmSensitive.setCreatedTime(new Date());
        return false;
    }

    /**
     * 敏感词查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listSensitive(PageRequestDto dto) {
        //参数校验
        dto.checkParam();
        //数据处理
        //执行分页
        Page<WmSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        //创建时间倒序;敏感词模糊查询
        page = page(page, Wrappers.<WmSensitive>lambdaQuery()
                .orderByDesc(WmSensitive::getCreatedTime)
                .like(dto.getName() != null, WmSensitive::getSensitives, dto.getName()));
        //封装返回
        ResponseResult res = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        res.setData(page.getRecords());
        return res;
    }

    /**
     * 敏感词修改
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        //参数校验
        if (wmSensitive == null || StringUtils.isBlank(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //数据处理
        //敏感词已存在不能修改
        if (extracted(wmSensitive)) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    /**
     * 自管理敏感词审核
     *
     * @param content
     */
    public Map<String, Object> handleSensitiveScan(String content) {
        boolean flag = true;
        //获取数据库中自定义的敏感词
        List<WmSensitive> wmSensitives = wmSensitiveMapper.selectList(Wrappers.<WmSensitive>lambdaQuery().select(WmSensitive::getSensitives));
        List<String> sensitiveList = wmSensitives.stream().map(WmSensitive::getSensitives).collect(Collectors.toList());
        //初始化敏感词库
        SensitiveWordUtil.initMap(sensitiveList);
        //查看文章中是否包含敏感词.
        Map<String, Integer> res = SensitiveWordUtil.matchWords(content);
        String msg = "";
        if (res.size() > 0) {
            //存在敏感词
            msg = "存在敏感词:" + res;
            flag = false;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("flag", flag);
        map.put("msg",msg);
        return map;
    }
}
